/*
 Copyright (C) 2010 QDSS.org
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.qdss.bizz.privileges.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.qdss.bizz.privileges.DepthEntry;

/**
 * BizzDepthEntry
 * 
 * @author <a href="mailto:zhaofang123@gmail.com">Zhao Fangfang</a>
 * @since 0.2, 2010-10-4
 * @version $Id: BizzDepthEntry.java 7 2011-05-15 16:40:50Z zhaofang123@gmail.com $
 */
public class BizzDepthEntry implements DepthEntry {
	private static final long serialVersionUID = 7612024724703543451L;

	/**
	 * 全局 */
	public static final DepthEntry GLOBAL 	= new BizzDepthEntry("G", 4, null);
	/**
	 * 本部门及其所有下级部门 */
	public static final DepthEntry DEEPDOWN = new BizzDepthEntry("D", 3, GLOBAL);
	/**
	 * 本部门 */
	public static final DepthEntry LOCAL 	= new BizzDepthEntry("L", 2, DEEPDOWN);
	/**
	 * 本人私有 */
	public static final DepthEntry PRIVATE 	= new BizzDepthEntry("P", 1, LOCAL);
	/**
	 * 无 */
	public static final DepthEntry NONE 	= new BizzDepthEntry("N", 0, PRIVATE);

	private static final Map<Integer, DepthEntry> depthEntriesByMask = new HashMap<Integer, DepthEntry>(5);
	static {
		depthEntriesByMask.put(GLOBAL.getMask(), GLOBAL);
		depthEntriesByMask.put(DEEPDOWN.getMask(), DEEPDOWN);
		depthEntriesByMask.put(LOCAL.getMask(), LOCAL);
		depthEntriesByMask.put(PRIVATE.getMask(), PRIVATE);
		depthEntriesByMask.put(NONE.getMask(), NONE);
	}

	/**
	 * @param mask
	 * @return
	 * @throws IllegalArgumentException If illegal mask value
	 */
	public static DepthEntry parse(final int mask) throws IllegalArgumentException {
		DepthEntry de = depthEntriesByMask.get(mask);
		if (de == null)
			throw new IllegalArgumentException("No DepthEntry defined: " + mask);
		return de;
	}

	// -----------------------------------------------------------------------------------

	private final String name;
	private final int mask;

	private final DepthEntry previous;
	private DepthEntry next;

	/**
	 * @param name
	 * @param mask
	 * @param previous
	 */
	public BizzDepthEntry(String name, int mask, DepthEntry previous) {
		this.name = name;
		this.mask = mask;
		this.previous = previous;

		if (previous != null) {
			BizzDepthEntry bde = (BizzDepthEntry) this.previous;
			if (bde.next != null)
				throw new IllegalArgumentException("Could't re-defined next DepthEntry for BizzDepthEnter!");
			bde.next = this;
		}
	}

	@Override
	public Serializable getIdentity() {
		return new StringBuffer(getName()).append(':').append(getMask()).toString();
	}

	@Override
	public int getMask() {
		return mask;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public String toString() {
		return new StringBuffer().append(getIdentity()).append(":BizzDepthEntry@")
				.append(super.hashCode()).toString();
	}
}
